package com.cloud.servlet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.core.BaseServlet;
import com.cloud.dao.CourseBean;
import com.cloud.service.TempleteService;
import com.cloud.util.Code;
import com.cloud.util.CommUtil;
import com.cloud.util.JsonUtil;

/**
 * 1Cloud1
 * @author Administrator
 * @since 2016-1-16
 * @version 1.0 
 */
@Controller
@RequestMapping("/course")
public class TempletServlet extends BaseServlet {
	private static final Logger logger = LoggerFactory.getLogger(TempletServlet.class);
	@Autowired
	private TempleteService TempleteService;
	
	/**
	 * 上传病程
	 * @param request
	 * @param course
	 * @return
	 */
	@RequestMapping(value = "/upload", method = { RequestMethod.POST})
	public @ResponseBody Object upload(HttpServletRequest request, @RequestBody CourseBean course) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(course == null || CommUtil.isEmpty(course.getTempleteId()) || CommUtil.isEmpty(course.getPatientId()) || CommUtil.isEmpty(course.getRecordDate()) || CommUtil.isEmpty(course.getDataJson())){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		String json = course.getDataJson();
		logger.debug("===" + json);
		Map<String, String> paramMap = JsonUtil.getMapFromJson(json);
		String templeteId = paramMap.get("templetid");
		if(!templeteId.equals(course.getTempleteId())){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		String tableName = TempleteService.getTableName(templeteId);
		if(CommUtil.isEmpty(tableName)){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		course.setTableName(tableName);
		paramMap.put("tableName", tableName);
		List<String> columnList = TempleteService.getColumnList(templeteId);
		StringBuffer columnBuffer = new StringBuffer("templet_id,patient_id,record_date,course_json");
		StringBuffer valueBuffer = new StringBuffer();
		valueBuffer.append(",").append(templeteId).append("");
		valueBuffer.append(",").append(course.getPatientId()).append("");
		valueBuffer.append(",'").append(course.getRecordDate()).append("'");
		valueBuffer.append(",'").append(course.getDataJson()).append("'");
		for(String column : columnList){
			columnBuffer.append(",").append(column);
			valueBuffer.append(",'").append(paramMap.get(column)).append("'");
		}
		paramMap.put("columnNames", columnBuffer.toString());
		paramMap.put("values", valueBuffer.toString().substring(1));
		TempleteService.deleteCourse(course);
		TempleteService.addCourse(paramMap);
		paramMap.put("tableName", tableName+"_his");
		TempleteService.addCourse(paramMap);
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
	
	/**
	 * 获取病程
	 * @param request
	 * @param course
	 * @return
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.GET})
	public @ResponseBody Object list(HttpServletRequest request, CourseBean course) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(course == null || CommUtil.isEmpty(course.getId()) || CommUtil.isEmpty(course.getPatientId())){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		String tableName = TempleteService.getTableNameByPro(course);
		if(CommUtil.isEmpty(tableName)){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		course.setTableName(tableName);
		List<CourseBean> beanList = TempleteService.getCourseList(course);
		List<String> retList = new ArrayList<String>();
		if(beanList != null){
			for(CourseBean bean : beanList){
				retList.add(bean.getDataJson());
			}
		}
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		map.put("data", retList);
		return map;
	}
}
